#!/bin/sh
# Copyright 2018 IBM Corp
# SPDX-License-Identifier: Apache-2.0
# Authored May 2018, Joel Stanley <joel@jms.id.au>
#
# This script sets the SIO scratch registers 0x2D in order to configure
# hostboot. It supports boot flags v1 as defined in hostboot source:
#  src/usr/initservice/bootconfig/bootconfig_ast2400.C
#  src/usr/console/ast2400.C
#
# BOOT_FLAGS_VERSION_REG    = 0x28,
# Serial config reg: 0x2d
# Serial config mask: 0xc0
#
#  NONE            = 0x00,  // No output selected
#  SELECT_SUART    = 0x40,  // SIO Uart
#  SELECT_VUART    = 0x80,  // SOL virtual uart
#  RESERVED        = 0xc0,  // Reserved


SYSFS_SIO=/sys/devices/platform/ahb/ahb:apb/1e789000.lpc/1e789080.lpc-host/1e789080.lpc-host:regs
SYSFS_SIO28=$SYSFS_SIO/sio_28
SYSFS_SIO2D=$SYSFS_SIO/sio_2d

FLAGS_VERSION1=$((0x42))

usage()
{
	echo "usage: hb_settings [[-u|--uart vuart|suart|none] | [-s|--show] | [-h]]"
}

show_regs()
{
	SIO28=$(cat $SYSFS_SIO28)
	SIO2D=$(cat $SYSFS_SIO2D)

	case $SIO28 in
		$FLAGS_VERSION1)
			echo "Boot flags version 1"
			;;
		* )
			echo "Unknown boot flags version"
			;;
	esac

	case $(($SIO2D >> 6)) in
		0)
			echo "Hostboot serial output disabled"
			;;
		1)
			echo "Hostboot serial output on SUART"
			;;
		2)
			echo "Hostboot serial output on VUART"
			;;
		3)
			echo "Reserved value"
			;;
		* )
			echo "Invalid uart value"
			;;
	esac
}

set_regs()
{
	case $uart in
		suart)
			echo "Hostboot serial output on SUART"
			VAL=0x40
			;;
		vuart)
			echo "Hostboot serial output on VUART"
			VAL=0x80
			;;
		none)
			echo "Hostboot serial output disabled"
			VAL=0x00
			;;
		* )
			echo "Invalid uart value"
			usage
			exit 1
	esac

    echo $FLAGS_VERSION1 > $SYSFS_SIO28
    echo $VAL > $SYSFS_SIO2D
}

while [ "$1" != "" ]; do
    case $1 in
	    -u | --uart)	shift
		    		uart=$1
				set_regs
				exit
		    		;;
	    -s | --show )	show_regs
		    		exit
		    		;;
	    -h | --help )	usage
		    		exit
		    		;;
	    * )			usage
                                exit 1
    esac
    shift
done

usage
exit 0
